package com.firebird.app.kotlin.stock.ui.free_interset

import android.R
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import butterknife.BindView
import butterknife.OnCheckedChanged
import butterknife.OnClick
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.utils.BaseUtils.bit1
import com.firebird.app.kotlin.stock.utils.DialogUtils.closeDialog
import com.firebird.app.kotlin.stock.utils.DialogUtils.showDialog_progressbar
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils.restLoginDialog
import com.firebird.app.kotlin.stock.utils.ToastUtil.showToast
import com.xujiaji.happybubble.BubbleDialog
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.ui.free_interset.contract.FreeIntersetContract
import com.firebird.app.kotlin.stock.ui.free_interset.presenter.FreeIntersetPresenter
import com.firebird.app.kotlin.stock.ui.success.AllocationSuccessActivity
import com.firebird.app.kotlin.stock.utils.BaseUtils
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil


/**
 * 免息配资
 */
class FreeintersetActivity : BaseMvpActivity<FreeIntersetPresenter?>(), FreeIntersetContract.View {
    @BindView(R.id.back)
    var back: LinearLayout? = null
    @BindView(R.id.tv_title)
    var tvTitle: TextView? = null
    @BindView(R.id.cashdeposit)
    var cashdeposit: TextView? = null
    @BindView(R.id.interset)
    var interset: TextView? = null
    @BindView(R.id.totlemoney)
    var totlemoney: TextView? = null
    @BindView(R.id.text1)
    var text1: TextView? = null
    @BindView(R.id.text2)
    var text2: TextView? = null
    @BindView(R.id.iv_warning)
    var ivWarning: ImageView? = null
    @BindView(R.id.text3)
    var text3: TextView? = null
    @BindView(R.id.iv_close)
    var ivClose: ImageView? = null
    @BindView(R.id.text4)
    var text4: TextView? = null
    @BindView(R.id.text5)
    var text5: TextView? = null
    @BindView(R.id.text6)
    var text6: TextView? = null
    @BindView(R.id.cb_agree)
    var cbAgree: CheckBox? = null
    @BindView(R.id.look_agree)
    var lookAgree: TextView? = null
    @BindView(R.id.tv_apply)
    var tvApply: TextView? = null
    private var mIndex = 0
    private var bundle: Bundle? = null
    private var strLilv: Double? = null
    private var strBaozheng: Double? = null
    private var strPeizi: Double? = null
    private var timeType = 0
    private var bpid = 0
    private var bei = 0
    private var bpAround = 0
    private var baozheng: String? = null
    private var lilv: String? = null
    private var peizi: String? = null
    private var dialog: Dialog? = null
    override val layoutId: Int
        get() = R.layout.activity_freeinterset

    override fun initView() {
        bundle = intent.extras
        mIndex = bundle.getInt("mIndex")
        timeType = bundle.getInt("timetype")
        bpid = bundle.getInt("bpid")
        bei = bundle.getInt("bei")
        baozheng = bundle.getString("baozheng")
        lilv = bundle.getString("lilv")
        peizi = bundle.getString("peizi")
        strBaozheng = baozheng.toDouble()
        strLilv = lilv.toDouble()
        strPeizi = peizi.toDouble()
        //保证金
        cashdeposit!!.text = baozheng
        //利息
        val lixi = strPeizi!! * strLilv!! / 100
        interset!!.text = bit1(lixi)
        //准备资金
        val zhunbei = strBaozheng!! + lixi
        totlemoney!!.text = bit1(zhunbei)
        //配资资金
        text1!!.text = peizi
        //操盘总额
        text2!!.text = (strPeizi!! + strBaozheng!!).toString() + ""
        //预警线=配资资金+保证金×0.5%
        text3!!.text = (strBaozheng!! * 0.5 / 100 + strPeizi!!).toString() + ""
        //平仓线=配资资金+保证金×0.3%
        text4!!.text = (strBaozheng!! * 0.3 / 100 + strPeizi!!).toString() + ""
        //资金日利率
        text6!!.text = "$lilv%"
        when (mIndex) {
            Config.FREE -> {
                tvTitle!!.text = "免息配资"
                bpAround = 2
            }
            Config.DAY -> {
                tvTitle!!.text = "按日配资"
                bpAround = 2
            }
            Config.MON -> {
                tvTitle!!.text = "按月配资"
                bpAround = 1
            }
        }
        mPresenter = FreeIntersetPresenter()
        mPresenter.attachView(this)
    }

    override fun showLoading() {
        dialog = showDialog_progressbar(this)
    }

    override fun hideLoading() {
        closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        closeDialog(dialog)
    }

    fun onSuccess(bean: SimpleBean) {
        if (bean.respCode == Config.SUCCESS) {
            toClass1(this, AllocationSuccessActivity::class.java)
        } else if (bean.respCode == Config.TOKENOUT) {
            restLoginDialog(this)
        } else {
            showToast(this, bean.respDesc)
        }
    }

    @OnCheckedChanged(R.id.cb_agree)
    fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
        when (buttonView.id) {
            R.id.cb_agree -> if (isChecked) {
                tvApply!!.isEnabled = true
                tvApply!!.alpha = 1f
            } else {
                tvApply!!.isEnabled = false
                tvApply!!.alpha = 0.5f
            }
        }
    }

    @OnClick(R.id.back, R.id.iv_warning, R.id.iv_close, R.id.look_agree, R.id.tv_apply)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.back -> finish()
            R.id.iv_warning -> BubbleDialog(this)
                    .addContentView(LayoutInflater.from(this).inflate(R.layout.boom_warm, null))
                    .setClickedView(ivWarning)
                    .setOffsetY(-15)
                    .show()
            R.id.iv_close -> BubbleDialog(this)
                    .addContentView(LayoutInflater.from(this).inflate(R.layout.boom_close, null))
                    .setClickedView(ivClose)
                    .setOffsetY(-15)
                    .show()
            R.id.look_agree -> {
            }
            R.id.tv_apply -> mPresenter.getData(baozheng, bpid, bei, mIndex, lilv, bpAround, timeType)
        }
    }
}